import ujson
import datetime

from pika import data

from 获取监测点信息 import *
from 操作消息队列 import *

'''
     * {
     * "labEquipmentNumber":"设备编号",
     * "data":[ {"温度":"3.4","湿度":"5.5","时间":"2023-09-25 12:45:45"}]
     * }
'''
def msg(**kwargs):
    # 获取当前时间
    now = datetime.datetime.now()
    nodes = select_scadaequipnodeinfo(kwargs['equipmentNumber'])
    acode = kwargs['acode']
    values = kwargs['values'].split(',')

    json = {}
    json['labEquipmentNumber'] = kwargs['equipmentNumber']

    # 根据nodes的元素个数进行循环
    json2 = {}
    for i in range(len(nodes)):
        # 获取第i个元素
        node = nodes[i]
        nodetype = node[2]
        if nodetype == '时间':
            json2[nodetype] = str(now)
        else:
            json2[nodetype] = values[i]
    data = []
    data.append(json2)

    json['data'] = data


    json_str = ujson.dumps(json, ensure_ascii=False)
    print(json_str)
    return json_str

def send_msg(**kwargs):
    json_str = msg(**kwargs)
    queueName = kwargs['queueName']
    # 上面的发送消息代码，循环调用100次
    connection,channel = conn_rabbitmq()
    for i in range(int(kwargs['count'])):
        channel.basic_publish(exchange='', routing_key=queueName, body=json_str)
        # 输出显示：发送完成：json_str
        print(str(i)+"发送完成：" + json_str)
    # 关闭连接
    connection.close()



if __name__ == '__main__':
    params = {
        'equipmentNumber':'A2201020018220321',
        'acode':'A2201020018220321',
        'queueName':'prod_dzgs_equip_scada_msg',
        'workLocation':'',
        'testFormNumber':'',
        'count':'100',
        'values':'97,98,34,56,76,56'
    }
    send_msg(**params)